<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" stripe style="width: 100%">
        <el-table-column
          prop="stem"
          label="标题"
          width="350px"
        ></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column
          prop="createdAt"
          label="更新时间"
          width="180px"
        ></el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>

              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      :before-close="handleClose"
      size="50%"
    >
      <!-- 抽屉内容区 -->
      <div v-if="drawerType === 'view'">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form v-else :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          >
            <!-- 验证表单是否有内容 -->
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onAdd">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  getArticleList,
  delArticle,
  addArticle,
  getArticle,
  updataArticle
} from '@/api/article'
export default {
  name: 'article-page',
  components: {
    // 注册富文本编辑器
    quillEditor
  },
  data () {
    return {
      current: 1,
      pageSize: 10,
      total: 0, // 数据总数
      list: [],
      // 抽屉开关
      drawer: false,
      direction: 'rtl',
      // 抽屉标题
      drawerType: 'type',
      // 表单
      form: {
        stem: '', // 面试题标题
        content: '' // 面试题内容
      },
      rules: {
        stem: [{ required: true, message: '标题必填', trigger: 'blur' }],
        content: [{ required: true, message: '内容必填', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.initData()
  },
  computed: {
    // 抽屉大标题
    drawerTitle () {
      let title = '大标题'
      if (this.drawerType === 'add') {
        title = '添加面经'
      } else if (this.drawerType === 'view') {
        title = '面经预览'
      } else if (this.drawerType === 'edit') {
        title = '面经修改'
      }
      return title
    }
  },
  methods: {
    // 最初页面获取数据
    async initData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 切换页码进行跳转
    handleCurrentChange (val) {
      // console.log(val),
      this.current = val
      this.initData()
    },
    // 打开抽屉的方法
    handleClose () {
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.drawer = false
    },
    // 打开不同类型的抽屉
    async openDrawer (type, id) {
      // 数据回填
      // console.log(id)
      this.drawer = true
      this.drawerType = type
      if (type !== 'add') {
        const res = await getArticle(id)
        // console.log(res)
        this.form.stem = res.data.stem
        this.form.content = res.data.content
        this.form.id = res.data.id
        // this.form = { ...res.form }
      }
      // console.log(type)
    },
    // 删除面经
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delArticle(id)
          this.initData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //
    onAdd () {
      this.$refs.form.validate(async (vaild) => {
        if (vaild) {
          if (this.drawerType === 'add') {
            const { stem, content } = this.form
            await addArticle({ stem, content })
            this.$message.success('添加成功')
          } else {
            // form表单没拿到id
            const { id, stem, content } = this.form
            console.log(id)
            await updataArticle({ id, stem, content })
            this.$message.success('修改成功')
          }
          // await addArticle(this.form)
          // this.$message.success('添加成功')
          this.handleClose()
          this.initData()
          this.current = 1
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
